#include "widget.h"
#include "./ui_widget.h"
#include <QAbstractItemView>
#include <QGroupBox>
#include <QKeyEvent>

void Widget::refreshToUI(QGroupBox *curGroupBox)
{
    ui->chkAcceptDrops->setChecked(m_itemView->acceptDrops());
    ui->chkDragEnabled->setChecked(m_itemView->dragEnabled());

    ui->comboMode->setCurrentIndex(m_itemView->dragDropMode());
    int index = getDropActionIndex(m_itemView->defaultDropAction());
    ui->comboAction->setCurrentIndex(index);

    QFont font = ui->groupBox_listSource->font();
    font.setBold(false);
    ui->groupBox_listSource->setFont(font);
    ui->groupBox_listWidget->setFont(font);
    ui->groupBox_treeWidget->setFont(font);
    ui->groupBox_tableWidget->setFont(font);

    font.setBold(true);
    curGroupBox->setFont(font);
}

int Widget::getDropActionIndex(Qt::DropAction actionType)
{
    int res = 0;
    switch(actionType) {
    case Qt::CopyAction:
        res = 0;
        break;
    case Qt::MoveAction:
        res = 1;
        break;
    case Qt::LinkAction:
        res = 2;
        break;
    case Qt::IgnoreAction:
        res = 3;
        break;
    default:
        res = 0;
    }
    return res;
}

Qt::DropAction Widget::getDropActionType(int index)
{
    //    CopyAction = 0x1,
    //    MoveAction = 0x2,
    //    LinkAction = 0x4,
    //    ActionMask = 0xff,
    //    TargetMoveAction = 0x8002,
    //    IgnoreAction = 0x0
    Qt::DropAction res = Qt::CopyAction;
    switch(index) {
    case 0:
        res = Qt::CopyAction;
        break;
    case 1:
        res = Qt::MoveAction;
        break;
    case 2:
        res = Qt::LinkAction;
        break;
    case 3:
        res = Qt::IgnoreAction;
        break;
    default:
        ;
    }
    return res;
}

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);

    ui->radioListSource->setChecked(true);
    m_itemView = ui->listSource;

    ui->listSource->setAcceptDrops(true);
    ui->listSource->setDragEnabled(true);
    ui->listSource->setDefaultDropAction(Qt::CopyAction);
    ui->listSource->setDragDropMode(QAbstractItemView::DragDrop);

    ui->listWidget->setAcceptDrops(true);
    ui->listWidget->setDragEnabled(true);
    ui->listWidget->setDefaultDropAction(Qt::CopyAction);
    ui->listWidget->setDragDropMode(QAbstractItemView::DragDrop);

    ui->treeWidget->clear();
    ui->treeWidget->setAcceptDrops(true);
    ui->treeWidget->setDragEnabled(true);
    ui->treeWidget->setDefaultDropAction(Qt::CopyAction);
    ui->treeWidget->setDragDropMode(QAbstractItemView::DragDrop);

    ui->tableWidget->setAcceptDrops(true);
    ui->tableWidget->setDragEnabled(true);
    ui->tableWidget->setDefaultDropAction(Qt::MoveAction);
    ui->tableWidget->setDragDropMode(QAbstractItemView::DragDrop);

    ui->listSource->installEventFilter(this);
    ui->listWidget->installEventFilter(this);
    ui->treeWidget->installEventFilter(this);
    ui->tableWidget->installEventFilter(this);
}

Widget::~Widget()
{
    delete ui;
}

void Widget::on_radioListSource_clicked()
{
    m_itemView = ui->listSource;
    refreshToUI(ui->groupBox_listSource);
}

void Widget::on_radioListWidget_clicked()
{
    m_itemView = ui->listWidget;
    refreshToUI(ui->groupBox_listWidget);
}


void Widget::on_radioTreeWidget_clicked()
{
    m_itemView = ui->treeWidget;
    refreshToUI(ui->groupBox_treeWidget);
}


void Widget::on_radioTableWidget_clicked()
{
    m_itemView = ui->tableWidget;
    refreshToUI(ui->groupBox_tableWidget);
}

void Widget::on_chkAcceptDrops_clicked(bool checked)
{
    m_itemView->setAcceptDrops(checked);
}

void Widget::on_chkDragEnabled_clicked(bool checked)
{
    m_itemView->setDragEnabled(checked);
}

void Widget::on_comboMode_currentIndexChanged(int index)
{
    m_itemView->setDragDropMode(static_cast<QAbstractItemView::DragDropMode>(index));
}

void Widget::on_comboAction_currentIndexChanged(int index)
{
    m_itemView->setDefaultDropAction(getDropActionType(index));
}

bool Widget::eventFilter(QObject *watched, QEvent *event)
{
    if (event->type() != QEvent::KeyPress){
        return QWidget::eventFilter(watched, event);
    }

    QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
    if (keyEvent->key() != Qt::Key_Delete) {
        return QWidget::eventFilter(watched, event);
    }

    if (watched == ui->listSource) {
        delete ui->listSource->takeItem(ui->listSource->currentRow());
    }
    else if (watched == ui->listWidget) {
        delete ui->listWidget->takeItem(ui->listWidget->currentRow());
    }
    else if (watched == ui->treeWidget) {
        QTreeWidgetItem *curItem = ui->treeWidget->currentItem();
        if (curItem->parent() != nullptr) { // not top level
            curItem->parent()->removeChild(curItem);
        }
        else {
            int index = ui->treeWidget->indexOfTopLevelItem(curItem);
            ui->treeWidget->takeTopLevelItem(index);
        }
        delete curItem;
    }
    else if (watched == ui->tableWidget) {
        delete ui->tableWidget->takeItem(ui->tableWidget->currentRow(),
                                         ui->tableWidget->currentColumn());
    }

    return true;
}
